﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace Ayatta.Contract.Domain
{
    public class ProductItemSku : IEntity<int>
    {
        /// <summary>
        /// sku的id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 商家Id
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// sku所属商品id
        /// </summary>
        public int ItemId { get; set; }

        /// <summary>
        /// 所属产品Id
        /// </summary>
        public int ProductId { get; set; }

        /// <summary>
        /// 根类目Id
        /// </summary>
        public int CategoryRootId { get; set; }

        /// <summary>
        /// 类目Id
        /// </summary>
        public int CategoryId { get; set; }

        /// <summary>
        /// 类目字符串
        /// </summary>
        public string CategoryMiddleId { get; set; }

        /// <summary>
        /// 商家设置的外部id
        /// </summary>
        public string OuterId { get; set; }

        /// <summary>
        /// sku的销售属性组合字符串（颜色，大小，等等，可通过类目API获取某类目下的销售属性）,格式是p1:v1;p2:v2
        /// </summary>
        public string PropId { get; set; }

        /// <summary>
        /// sku所对应的销售属性的中文名字串，格式如：pid1:vid1:pid_name1:vid_name1;pid2:vid2:pid_name2:vid_name2……
        /// </summary>
        public string PropStr { get; set; }

        /// <summary>
        /// 属于这个sku的商品的数量
        /// </summary>
        public int Quantity { get; set; }

        /// <summary>
        /// 属于这个sku的商品的价格 取值范围:0-100000000;精确到2位小数;单位:元
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// 销售量
        /// </summary>
        public int Sale { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public byte Status { get; set; }

        /// <summary>
        /// 创建日期时间
        /// </summary>
        public DateTime CreatedOn { get; set; }

        /// <summary>
        /// 最后一次编辑日期时间
        /// </summary>
        public DateTime ModifiedOn { get; set; }

        public string[] PropTexts
        {
            get
            {
                return !string.IsNullOrEmpty(PropStr) ? PropStr.Split(';').Select(o => o.Split(':')[2].Trim() + "：" + o.Split(':')[3].Trim()).ToArray() : null;
            }
        }

        public IList<SkuProp> SkuProps
        {
            get
            {
                var list = new List<SkuProp>();
                if (!string.IsNullOrEmpty(PropStr))
                {
                    var array = PropStr.Split(';');
                    foreach (var s in array)
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            var temp = s.Split(':');
                            SkuProp roperty;
                            roperty.PId = Convert.ToInt32(temp[0]);
                            roperty.VId = Convert.ToInt32(temp[1]);
                            roperty.PName = temp[2];
                            roperty.VName = temp[3];
                            list.Add(roperty);
                        }
                    }
                }
                return list;
            }
        }
        

        public struct SkuProp
        {
            public int PId;
            public string PName;
            public int VId;
            public string VName;
        }
    }
}
